#!/bin/tcsh -fe
####
#
#############################################################################
#                                                                           #
# Title: Import Movie from EPU                                    		    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 09/06/2017                                             #
# Last Modification: 16/01/2019                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 30
#
# MANUAL: <B>Welcome to the File Importer</B>
#
# DISPLAY: imagename_original
# DISPLAY: imagenumber
# DISPLAY: tempkeep
# DISPLAY: comment
# DISPLAY: crop_histogram
# DISPLAY: crop_histogram_stdev
# DISPLAY: import_rawstack
# DISPLAY: import_rawstack_original
# DISPLAY: import_rawstack_type
# DISPLAY: import_gainref
# DISPLAY: import_gainref_original
# DISPLAY: import_defects
# DISPLAY: import_defects_original
# DISPLAY: raw_gaincorrectedstack
# DISPLAY: magnification
# DISPLAY: stepdigitizer
# DISPLAY: Calc_from_sample_pixel
# DISPLAY: sample_pixel
# DISPLAY: take_header_orig_sample_pixel
# DISPLAY: orig_sample_pixel
# DISPLAY: movie_imagenumber_total
# DISPLAY: import_bin
# DISPLAY: import_bin_target
# DISPLAY: movie_image_dose_source
# DISPLAY: movie_image_dose_factor
# DISPLAY: movie_image_dose_manually
# DISPLAY: frame_measured_image_dose
# DISPLAY: frame_image_dose
# DISPLAY: frame_dose
# DISPLAY: total_dose
# DISPLAY: CS
# DISPLAY: KV
# DISPLAY: Thread_Number
# DISPLAY: EPU_XML_filename
# DISPLAY: EPU_dirname
#
#$end_local_vars
#
# Static directory variables at disposition are:
# appDir_2dx
# scripts-standard_2dx
# scripts-custom_2dx
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
# Variables to be available from the config file are:
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set crop_histogram = ""
set crop_histogram_stdev = ""
set movie_stackname = ""
set import_rawstack = ""
set import_gainref = ""
set import_gainref_original = ""
set import_rawstack_type = ""
set import_defects = ""
set import_defects_original = ""
set raw_gaincorrectedstack = ""
set magnification = ""
set stepdigitizer = ""
set Calc_from_sample_pixel = ""
set sample_pixel = ""
set take_header_orig_sample_pixel = ""
set orig_sample_pixel = ""
set movie_imagenumber_total = ""
set import_bin = ""
set import_bin_target = ""
set import_rawstack_original = ""
set SERIALEM_FACTOR = ""
set SERIALEM_DEFAULT_FACTOR = ""
set import_original_time = ""
set frame_image_dose_source = ""
set frame_image_dose_factor = ""
set frame_image_dose_manually = ""
set movie_image_dose_source = ""
set movie_image_dose_factor = ""
set movie_image_dose_manually = ""
set frame_measured_image_dose = ""
set CS = ""
set KV = ""
set defocus_phase_shift_doit = ""
set Thread_Number = ""
set export_basedir = ""
set export_rawstack_subdir = ""
set EPU_XML_filename = ""
set EPU_dirname = ""
set import_file_status = ""
#
#$end_vars
#
set scriptname = import_epu
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
source ${proc_2dx}/2dx_makedirs
#
set import_produce_gainref2D = "y"
set import_produce_gainref2Dfft = "y"
#
setenv OMP_NUM_THREADS ${Thread_Number}
#
if ( ${website_blur_doit} == "y" ) then
  set blurkernel = ${website_blurkernel}
else
  set blurkernel = 0
endif
#
if ( ${EPU_XML_filename}x == "x" ) then
  set EPU_XML_filename = ${import_rawstack:r}.xml
  echo "set EPU_XML_filename = ${EPU_XML_filename}" >> LOGS/${scriptname}.results
endif
#
if ( ! -e ${EPU_XML_filename} ) then
  if ( ! -e ${import_rawstack:r}.xml ) then
	if ( -e ${import_rawstack_original:r}.xml ) then
		\cp -f ${import_rawstack_original:r}.xml ${EPU_XML_filename}
	else
	    #################################################################################
	    ${proc_2dx}/protest "${EPU_XML_filename} not found!"
	    #################################################################################
	endif
  endif
endif

echo "<<@progress: 10>>"
#
if ( ${CS} == "ScriptWillPutNumberHere" ) then
  set CS = ${Default_CS}
  echo "set CS = ${CS}" >> LOGS/${scriptname}.results
endif
#
if ( ${KV} == "ScriptWillPutNumberHere" ) then
  set KV = ${Default_KV}
  echo "set KV = ${KV}" >> LOGS/${scriptname}.results
endif
#
if ( ${defocus_phase_shift_doit} == "-" ) then
  set defocus_phase_shift_doit = ${Default_phase_shift_doit}
  echo "set defocus_phase_shift_doit = ${defocus_phase_shift_doit}" >> LOGS/${scriptname}.results
endif
#
if(${import_original_time} == "-" || ${import_original_time} == "") then
  @ status_date = `date +%s` * 1000
  set date_text = "Processed at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | gawk '{print strftime("%c", $0)}' `
  # set date_text = "Processed at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | awk 'BEGIN{m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|") for(o=1;o<=m;o++){ months[d[o]]=sprintf("%02d",o)} format = "%m/%d/%Y %H:%M" } {split($4,time,":") date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0") print strftime(format, mktime(date))}'`
  echo "set import_original_time = ${status_date}" >> LOGS/${scriptname}.results
else
  set status_date = ${import_original_time}
  set date_text = "Recorded at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | gawk '{print strftime("%c", $0)}' `
  # set date_text = "Recorded at "`echo ${status_date} | awk '{ s = $1 / 1000 } END { print s }' | awk 'BEGIN{m=split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",d,"|") for(o=1;o<=m;o++){ months[d[o]]=sprintf("%02d",o)} format = "%m/%d/%Y %H:%M" } {split($4,time,":") date = (strftime("%Y") " " months[$2] " " $3 " " time[1] " " time[2] " 0") print strftime(format, mktime(date))}'`
endif
#
if ( ! -e ${import_rawstack} ) then
  if ( -e ${import_rawstack_original} ) then
    rsync -auvP ${import_rawstack_original}  ${import_rawstack}
  endif
endif
#
if ( ! -e ${raw_gaincorrectedstack}.mrcs ) then
  if ( -e ${import_rawstack} ) then
    rsync -auvP ${import_rawstack} ${raw_gaincorrectedstack}.mrcs
  endif
endif
#


if ( ${import_rawstack_type} == "1" ) then
  if ( ! -e ${import_rawstack} ) then
    set import_rawstack_exportname = ${export_basedir}/${export_rawstack_subdir}/${import_rawstack}
    ${proc_2dx}/linblock "ERROR: Raw stack ${import_rawstack} not found. Trying to find it in ${import_rawstack_exportname}"
    if ( -e ${import_rawstack_exportname} ) then
      rsync -auvP ${import_rawstack_exportname} ${import_rawstack}
      ${proc_2dx}/linblock "Raw stack found and copied back."
    else
      ${proc_2dx}/protest "ERROR: Raw stack ${import_rawstack} not found."
    endif
  endif    
  if ( "${import_gainref}" != "" ) then
    if ( ! -e "${import_gainref}" ) then
      if ( "${import_gainref_original}"x != "x" ) then
        if ( ! -e "${import_gainref_original}" ) then
          ${proc_2dx}/protest "ERROR: Gain reference "${import_gainref}" not found."
        else
          ${proc_2dx}/linblock "WARNING: Gain reference missing. Copying it again here."
          \cp -f "${import_gainref_original}" .
          set import_gainref = `echo "${import_gainref_original}" | rev | cut -d\/ -f1 | rev `
          echo "set import_gainref = "${import_gainref}"" >> LOGS/${scriptname}.results
        endif
      else
        ${proc_2dx}/protest "WARNING: Gain reference missing."
      endif
    endif
  endif
  echo "# IMAGE-IMPORTANT: ${import_rawstack} <Raw, dark-sub. stack (stack)>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: "${import_gainref}" <Gain reference (2D)>" >> LOGS/${scriptname}.results
  echo "<<@evaluate>>"
  echo "::Raw movie as dark-subtracted stack present. Gain reference found."
  #
  set import_rawstack_end = `echo ${import_rawstack} | rev | cut -c1-4 | rev`
  if ( ${import_rawstack_end} == "mrcs" ) then
    set SERIALEM_FACTOR = 1.0
  else if ( ${import_rawstack_end} == ".mrc" ) then
    set SERIALEM_FACTOR = 1.0
  else if ( ${import_rawstack_end} == ".tif" ) then
    set SERIALEM_FACTOR = ${SERIALEM_DEFAULT_FACTOR}
  else
    ${proc_2dx}/protest "ERROR: file format ${import_rawstack_end} not supported."
  endif
  # 
# if ( ${do_unpack} == "y" ) then
if ( ${import_file_status} == 0 ) then
    # set float = " "
    set float = "-m 2"
    if ( "${import_defects}"x != "x" ) then
      if ( -e "${import_defects}" ) then
        #################################################################################
        ${proc_2dx}/linblock "Running clip unpack ${float} -D "${import_defects}" ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs"
        #################################################################################
        #
        \rm -f FASTDISK/${raw_gaincorrectedstack}.mrcs
        time nice ${dir_imod}/bin/clip unpack ${float} -D "${import_defects}" ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs
      else
        #################################################################################
        ${proc_2dx}/linblock "Running clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs"
        #################################################################################
        #
        ${proc_2dx}/linblock "WARNING: defects list file not found."
        \rm -f FASTDISK/${raw_gaincorrectedstack}.mrcs
        time nice ${dir_imod}/bin/clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs
      endif
    else
      #################################################################################
      ${proc_2dx}/linblock "Running clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs"
      #################################################################################
      #
      ${proc_2dx}/linblock "WARNING: defects list file not found."
      \rm -f FASTDISK/${raw_gaincorrectedstack}.mrcs
      time nice ${dir_imod}/bin/clip unpack ${float} ${import_rawstack} "${import_gainref}" FASTDISK/${raw_gaincorrectedstack}.mrcs
    endif
    #
    echo "set raw_gaincorrectedstack = ${raw_gaincorrectedstack}"  >> LOGS/${scriptname}.results
    echo "# IMAGE-IMPORTANT: FASTDISK/${raw_gaincorrectedstack}.mrcs <Gain-corrected stack (stack)>" >> LOGS/${scriptname}.results
  endif
endif
#
# \rsync -auvP ${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}.mrcs 

if ( ${take_header_orig_sample_pixel} == "y" ) then
# Here we read the header to get the original pixel size, and set sample_pixel for the first time.
	if ( "${import_gainref}"x != "x" ) then
	  #
	  set sample_pixel = `${dir_imod}/bin/clip info "${import_gainref}" | grep Scale | cut -d\( -f2 | cut -dx -f1` # In EPU files, the pixel size comes written on the header of the GAIN REFERENCE (alternatively, we could try reading it from the XML file)
	  echo ":: PIXEL SIZE IS ${sample_pixel}"
	  #
	else
	  #
	  echo "::Gain-corrected stack is ${raw_gaincorrectedstack}.mrcs"
	  set SERIALEM_FACTOR = ${SERIALEM_DEFAULT_FACTOR}
	  set sample_pixel = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep Scale | cut -d\( -f2 | cut -dx -f1` # In EPU files, the pixel size comes written on the header of the GAIN-CORRECTED STACK (alternatively, we could try reading it from the XML file)
	  #
	endif
	set orig_sample_pixel = ${sample_pixel}
else
# Or, the user provides the orig_sample_pixel and this becomes our initial sample_pixel.
	set sample_pixel = ${orig_sample_pixel}
endif
echo "set sample_pixel = ${sample_pixel}" >> LOGS/${scriptname}.results
#
set movie_imagenumber_total = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep "Image size"  | cut -d\, -f3 | cut -d\) -f1`
echo "set movie_imagenumber_total = ${movie_imagenumber_total}" >> LOGS/${scriptname}.results
#
if ( ${Calc_from_sample_pixel} == "y" ) then
  set magnification = `echo "scale=3; ${stepdigitizer} * 10000 / ${sample_pixel}" | bc ` 
  echo "set magnification = ${magnification}"  >> LOGS/${scriptname}.results
endif
#
set SERIALEM_FACTOR = 1
#
if ( ${import_bin} != "0" && ${import_bin} != "1" ) then
  set width = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep size | head -n 1 | cut -d\( -f2 | cut -d\, -f1`
  set height = `${dir_imod}/bin/clip info FASTDISK/${raw_gaincorrectedstack}.mrcs | grep size | head -n 1 | cut -d\( -f2 | cut -d\, -f2`
  set widthbig = `echo ${width} 4096 | awk '{ if ( $1 > $2 ) { s = 1 } else { s = 0 } } END { print s } '`
  if ( ${widthbig} == "1" ) then
    if ( ${import_bin} == "2" ) then
      set xdim = `echo ${width}  | awk '{ s = int($1 / 2) } END { print s }'`
      set ydim = `echo ${height} | awk '{ s = int($1 / 2) } END { print s }'`
      echo ":: Image has size ${width},${height}, which is too large. Fourier-cropping it to ${xdim},${ydim}."
      set sample_pixel = `echo ${sample_pixel} | awk '{ s = 2 * $1 } END { print s }'`
      echo "::SERIALEM_FACTOR is reduced by 4, to account for 2x2 Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} | awk '{ s = $1 / 4.0 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: resample_mp.exe"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      \rm -f dummy.mrc
      \ln -s FASTDISK/${raw_gaincorrectedstack}_large.mrcs dummy.mrc
      time nice +19 ${dir_frealign}/bin/resample_mp.exe << eot
dummy.mrc
FASTDISK/dummyout.mrc
no
no
${xdim}
${ydim}
eot
      if ( -e FASTDISK/dummyout.mrc ) then
        \mv -f FASTDISK/dummyout.mrc FASTDISK/${raw_gaincorrectedstack}.mrcs
      else
        ${proc_2dx}/protest "ERROR occured in frealign resample_mp.exe"
      endif
      \rm -f dummy.mrc
      #
    endif
    if ( ${import_bin} == "3" ) then
      #
      set xdim = `echo ${width}  | awk '{ s = int($1 / 2) } END { print s }'`
      set ydim = `echo ${height} | awk '{ s = int($1 / 2) } END { print s }'`
      echo ":: Image has size ${width},${height}, which is too large. Fourier-cropping it to ${xdim},${ydim}."
      set sample_pixel = `echo ${sample_pixel} | awk '{ s = 2 * $1 } END { print s }'`
      echo "::SERIALEM_FACTOR is reduced by 4, to account for 2x2 Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} | awk '{ s = $1 / 4.0 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: newstack -ftreduce 2 -input ${raw_gaincorrectedstack}_large.mrcs -output ${raw_gaincorrectedstack}.mrcs"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      time nice +19 ${dir_imod}/bin/newstack -ftreduce 2 -mode 2 -input FASTDISK/${raw_gaincorrectedstack}_large.mrcs -output FASTDISK/${raw_gaincorrectedstack}.mrcs 
      #
    endif
    if ( ${import_bin} == "4" ) then
      #
      set factor = `echo ${sample_pixel} ${import_bin_target} | awk '{ s = $2 / $1 } END { print s }'`
      set factor2 = `echo ${factor} | awk '{ s = $1 * $1 } END { print s }'`
      set xdim = `echo ${width}  ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ydim = `echo ${height} ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ratio = `echo ${width} ${height} ${xdim} ${ydim} | awk '{ s = ($1 / $3) / ($2 / $4) } END { printf("%16.9f",s) }'`
      echo ":: Image has size ${width},${height} px at ${sample_pixel} A/px, which is too large. Fourier-cropping it to ${xdim},${ydim}, to reach ${import_bin_target} A/px."
      echo ":: Pixel aspect ratio will be 1:${ratio}."
      set sample_pixel = ${import_bin_target}
      echo "::SERIALEM_FACTOR is reduced by ${factor2}, to account for Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} ${factor2} | awk '{ s = $1 / $2 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: resample_mp.exe"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      \rm -f dummy.mrc
      \ln -s FASTDISK/${raw_gaincorrectedstack}_large.mrcs dummy.mrc
      time nice +19 ${dir_frealign}/bin/resample_mp.exe << eot
dummy.mrc
FASTDISK/dummyout.mrc
no
no
${xdim}
${ydim}
eot
      if ( -e FASTDISK/dummyout.mrc ) then
        \mv -f FASTDISK/dummyout.mrc FASTDISK/${raw_gaincorrectedstack}.mrcs
      else
        ${proc_2dx}/protest "ERROR occured in frealign resample_mp.exe"
      endif
      \rm -f dummy.mrc
      #
    endif
    if ( ${import_bin} == "5" ) then
      #
      set factor = `echo ${sample_pixel} ${import_bin_target} | awk '{ s = $2 / $1 } END { print s }'`
      set factor2 = `echo ${factor} | awk '{ s = $1 * $1 } END { print s }'`
      set xdim = `echo ${width}  ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ydim = `echo ${height} ${factor} | awk '{ s = $1 / $2 } END { printf("%12.0f",s) }'`
      set ratio = `echo ${width} ${height} ${xdim} ${ydim} | awk '{ s = ($1 / $3) / ($2 / $4) } END { printf("%16.9f",s) }'`
      echo ":: Image has size ${width},${height} px at ${sample_pixel} A/px, which is too large. Fourier-cropping to reach ${import_bin_target} A/px."
      set sample_pixel = ${import_bin_target}
      echo "::SERIALEM_FACTOR is reduced by ${factor2}, to account for Fourier cropping effect on mean dose per pixel."
      set SERIALEM_FACTOR = `echo ${SERIALEM_FACTOR} ${factor2} | awk '{ s = $1 / $2 } END { print s }'`
      #
      #################################################################################
      ${proc_2dx}/linblock "Running: fFourierCrop.exe"
      #################################################################################
      #
      \mv -f FASTDISK/${raw_gaincorrectedstack}.mrcs FASTDISK/${raw_gaincorrectedstack}_large.mrcs
      echo "# IMAGE: FASTDISK/${raw_gaincorrectedstack}_large.mrcs <Gain-corrected stack non-binned (stack)>" >> LOGS/${scriptname}.results
      #
      time nice +19 ${bin_2dx}/fFourierCrop.exe << eot
FASTDISK/${raw_gaincorrectedstack}_large.mrcs
FASTDISK/${raw_gaincorrectedstack}.mrcs
y
${import_bin_target}
doc.txt
n
eot
      set imagesidelength = `cat doc.txt | head -n 4 | tail -n 1 | cut -d\# -f1`
      echo "set imagesidelength = ${imagesidelength}" >> LOGS/${scriptname}.results
      #
    endif
  else
    echo ":: Image has size ${width}, which is small enough, no binning done."
  endif
  #
endif
#
echo "<<@progress: 30>>"
set total_dose_xml
if ( ${movie_image_dose_source} == "1" ) then
  set CountsToElectrons = `grep CountsToElectrons ${EPU_XML_filename} | head -n 1 | cut -d\> -f2 | cut -d\< -f1`
  set MeanPixelValue = `grep MeanPixelValue ${EPU_XML_filename} | head -n 1 | cut -d\> -f2 | cut -d\< -f1`
  set PixelValueToCameraCounts = `grep PixelValueToCameraCounts ${EPU_XML_filename} | head -n 1 | cut -d\> -f2 | cut -d\< -f1`
  echo ": CountsToElectrons = ${CountsToElectrons}"
  echo ": MeanPixelValue = ${MeanPixelValue}"
  # echo ": PixelValueToCameraCounts = ${PixelValueToCameraCounts}"
  set frame_image_dose = `echo "scale=3; ${CountsToElectrons} * ${MeanPixelValue} " | bc ` 
  # set frame_image_dose_factor = ${PixelValueToCameraCounts}
  if ( ${frame_image_dose_factor}x == "x" ) then
  	set dose_xml = `sed 's/>/>\n/g' ${EPU_XML_filename} | grep E+ | cut -d'E' -f1`
  	set total_dose_xml = `echo "scale=3; ${dose_xml} * 10 " | bc `
    # set frame_image_dose_factor = 1.0
    # echo ":: "
    # echo "::  WARNING: CountsToElectrons not found in .xml file! Using default of 1.0. May be inaccurate, try enforcing the dose manually!"
    # echo ":: "
  endif
else
	set frame_image_dose = `echo "scale=3; ${movie_image_dose_manually} / ${movie_imagenumber_total} " | bc `
endif
#


if ( ${total_dose_xml}x == 'x' ) then

	set frame_dose = ` echo "scale=8; ${frame_image_dose} / ${sample_pixel} / ${sample_pixel}" | bc `

	set total_dose = ` echo "scale=8; ${frame_dose} * ${movie_imagenumber_total} " | bc `

else

	set total_dose = ${total_dose_xml}
	set frame_dose = ` echo "scale=8; ${total_dose} / ${movie_imagenumber_total} " | bc `

endif

echo "::Calculated frame electron dose  is: ${frame_dose} electrons/A2/frame" 
echo "::Calculated total electron dose  is: ${total_dose} electrons/A2" 
echo "set frame_image_dose = ${frame_image_dose}" >> LOGS/${scriptname}.results
echo "set frame_dose = ${frame_dose}" >> LOGS/${scriptname}.results
echo "set total_dose = ${total_dose}" >> LOGS/${scriptname}.results
# echo "set frame_image_dose_factor = ${frame_image_dose_factor}"  >> LOGS/${scriptname}.results
#
if ( ${tempkeep} == "n" ) then
  \rm -f FASTDISK/${raw_gaincorrectedstack}_large.mrcs
endif
#
if ( ${import_produce_gainref2D} == "y" ) then
  #################################################################################
  ${proc_2dx}/linblock "Running: avgstack on ${raw_gaincorrectedstack}.mrcs"
  #################################################################################
  #
  ${dir_imod}/bin/avgstack << eot
FASTDISK/${raw_gaincorrectedstack}.mrcs
${raw_gaincorrectedstack}.mrc
/
eot
  echo "# IMAGE-IMPORTANT: ${raw_gaincorrectedstack}.mrc <Gain-corrected average image (2D)>" >> LOGS/${scriptname}.results
endif
#
echo "<<@progress: 50>>"
echo "<<@evaluate>>"
#
if ( ${crop_histogram} == "y" ) then
  set val_avg = `${dir_imod}/bin/clip stats ${raw_gaincorrectedstack}.mrc | head -n 3 | tail -n 1 | cut -d\)  -f3 | awk '{print $1}'`
  set val_stddev = `${dir_imod}/bin/clip stats ${raw_gaincorrectedstack}.mrc | head -n 3 | tail -n 1 | cut -d\)  -f3 | awk '{print $2}'`
  set newmin = `echo "scale=6; ${val_avg} - ${val_stddev} * ${crop_histogram_stdev}" | bc ` 
  set newmax = `echo "scale=6; ${val_avg} + ${val_stddev} * ${crop_histogram_stdev}" | bc ` 
  echo ":Statistics on 2D images are:  Average = ${val_avg}, StdDev = ${val_stddev}"
  #################################################################################
  ${proc_2dx}/linblock "Truncating histogram of 2D average to ${newmin} ... ${newmax}"
  #################################################################################
  #
  time nice +19 ${dir_imod}/bin/clip truncate -l ${newmin} -h ${newmax} ${raw_gaincorrectedstack}.mrc ${raw_gaincorrectedstack}_trunc.mrc
  \mv -f ${raw_gaincorrectedstack}_trunc.mrc ${raw_gaincorrectedstack}.mrc
endif
#
\rm -f tmp.png
\rm -f tmp2.png
\rm -f STATUS/1-image.jpg
${app_2dx_mrc_converter} --size 400 ${raw_gaincorrectedstack}.mrc tmp.png 
${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "Raw average"
${app_python} ${proc_2dx}/PNGannotator.py tmp2.png ${raw_gaincorrectedstack}.mrc.png 10 365 0 "${date_text}"
if ( ${blurkernel} != "0" ) then
  ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 135 180 ${blurkernel} "I N T E N T I O N A L L Y"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png tmp.png 135 195 0             "   B  L  U  R  R  E  D"
endif
${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "Raw average"
${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/1-image.jpg 10 365 0 "${date_text}"
\rm -f tmp.png 
\rm -f tmp2.png
#
if ( ${import_produce_gainref2D} == "y" && ${import_produce_gainref2Dfft} == "y" ) then
  #################################################################################
  ${proc_2dx}/linblock "Running: labelh.exe to normalize MRC image"
  #################################################################################
  # 
  echo "<<@progress: 45>>"
  #
  \rm -f FASTDISK/${raw_gaincorrectedstack}_norm.mrc
  #
  time ${bin_2dx}/labelh.exe << eot
${raw_gaincorrectedstack}.mrc
42
FASTDISK/${raw_gaincorrectedstack}_norm.mrc
eot
  #
  ${proc_2dx}/linblock "Running: clip fft FASTDISK/${raw_gaincorrectedstack}_norm.mrc ${raw_gaincorrectedstack}_fft.mrc"
  time nice +19 ${dir_imod}/bin/clip fft FASTDISK/${raw_gaincorrectedstack}_norm.mrc ${raw_gaincorrectedstack}_fft.mrc
  echo "# IMAGE-IMPORTANT: ${raw_gaincorrectedstack}_fft.mrc <Gain-corrected average image FFT (2D)>" >> LOGS/${scriptname}.results
  \rm -f FASTDISK/${raw_gaincorrectedstack}_norm.mrc
  \rm -f tmp.png
  \rm -f tmp2.png
  \rm -f STATUS/1-FFT.jpg
  ${app_2dx_mrc_converter} --size 400 ${raw_gaincorrectedstack}_fft.mrc tmp.png
  ${app_python} ${proc_2dx}/PNGannotator.py tmp.png tmp2.png 10 350 0 "FFT of raw average"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png ${raw_gaincorrectedstack}_fft.mrc.png 10 365 0 "${date_text}"
  ${app_python} ${proc_2dx}/PNGannotator.py tmp2.png STATUS/1-FFT.jpg 10 365 0 "${date_text}"
  \rm -f tmp.png
  \rm -f tmp2.png
endif
#
echo "set SERIALEM_FACTOR = ${SERIALEM_FACTOR}"  >> LOGS/${scriptname}.results
#
# This is needed for MotionCor2 later:
echo "set do_unpack = y" >> LOGS/${scriptname}.results
# 
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
exit
#
